package com.sec.android.app.myfiles.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.dialog.DownloadGear360ViewerDialog;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

public class Gear360ContentsExecutor {

    private static final String ViewerAppPackageName = "com.samsung.android.gear360viewer";
    private static final String ViewerAppImageViewerClassName = "com.samsung.android.gear360viewer.imageviewer360.ImagePlayer360Activity";
    private static final String ViewerAppVideoPlayerClassName = "com.samsung.android.gear360viewer.videoplayer360.VideoPlayer360Activity";

    private static final String ManagerAppPackageNameOld = "com.samsung.android.samsunggear360manager";
    private static final String ManagerAppPackageName = "com.samsung.android.gear360manager";
    private static final String ManagerAppImageViewerClassName = "com.samsung.android.samsunggear360manager.app.mediaplayer360.imageviewer360.ImagePlayer360Activity";
    private static final String ManagerAppVideoPlayerClassName = "com.samsung.android.samsunggear360manager.app.mediaplayer360.videoplayer360.VideoPlayer360Activity";

    private static final String KEY_MEDIA_PATH = "media-path";
    private static final String KEY_RECEIVE_FILE_PATH = "received_file_path";

    private static final String SamsungVideoPackageName = "com.samsung.android.video";
    private static final String SamsungVideoMoviePlayerClassName = "com.samsung.android.video.player.activity.MoviePlayer";

    private static final String SamsungGalleryPackageName = "com.sec.android.gallery3d";
    private static final String SamsungGalleryActivityClassName = "com.sec.android.gallery3d.app.GalleryActivity";

    public void executeGear360Contents(int processId, Activity activity, FileRecord record) {
        if (record.isVideoFileType()) {
            executeViaSamsungVideo(processId, activity, record);
        } else if (record.isImageFileType()) {
            executeViaSamsungGallery(processId, activity, record);
        } else {
            executeViaGear360Scenario(activity, record);
        }
    }

    private void executeViaSamsungGallery(int processId, Activity activity, FileRecord record) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setClassName(SamsungGalleryPackageName, SamsungGalleryActivityClassName);
            intent.setDataAndType(record.getUri(activity), record.getMimeType(activity));
            intent.putExtra("from-myfiles", true);
            activity.startActivity(intent);
            updateRecentFile(activity, record);
        } catch (ActivityNotFoundException e) {
            displayDownloadViewerDialog(activity, record);
        }
    }

    private void executeViaSamsungVideo(int processId, Activity activity, FileRecord record) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setClassName(SamsungVideoPackageName, SamsungVideoMoviePlayerClassName);
            intent.setDataAndType(record.getUri(activity), record.getMimeType(activity));
            intent.putExtra("from-myfiles", true);
            FileExecute.setVideoIntent(processId, intent, record, activity);
            activity.startActivity(intent);
            updateRecentFile(activity, record);
        } catch (ActivityNotFoundException e) {
            displayDownloadViewerDialog(activity, record);
        }
    }

    private void executeViaGear360Scenario(Activity activity, FileRecord record) {
        try {
            executeViaManager(activity, record);
        } catch (ActivityNotFoundException noManagerException) {
            try {
                executeViaViewer(activity, record);
            } catch (ActivityNotFoundException noViewerException) {
                displayDownloadViewerDialog(activity, record);
            }
        }
    }

    private void executeViaManager(Activity activity, FileRecord record) {
        String className;
        if (record.isImageFileType()) {
            className = ManagerAppImageViewerClassName;
        } else if (record.isVideoFileType()) {
            className = ManagerAppVideoPlayerClassName;
        } else {
            Log.e(this, "Unsupported file type");
            return;
        }
        Intent intent;
        // execution order
        // 1. New manager app - ManagerAppPackageName
        // 2. Legacy manager app - ManagerAppPackageNameOld
        try {
            intent = createIntent(activity, ManagerAppPackageName, className, record);
            intent.putExtra(KEY_MEDIA_PATH, record.getFullPath());
            activity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            intent = createIntent(activity, ManagerAppPackageNameOld, className, record);
            intent.putExtra(KEY_MEDIA_PATH, record.getFullPath());
            activity.startActivity(intent);
        }
        activity.overridePendingTransition(0, 0);
        updateRecentFile(activity, record);
    }

    private void executeViaViewer(Activity activity, FileRecord record) {
        String className;
        if (record.isImageFileType()) {
            className = ViewerAppImageViewerClassName;
        } else if (record.isVideoFileType()) {
            className = ViewerAppVideoPlayerClassName;
        } else {
            Log.e(this, "Unsupported file type");
            return;
        }
        Intent intent = createIntent(activity, ViewerAppPackageName, className, record);
        intent.putExtra(KEY_RECEIVE_FILE_PATH, record.getFullPath());

        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
        updateRecentFile(activity, record);
    }

    private Intent createIntent(Activity activity, String packageName, String className, FileRecord record) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setComponent(new ComponentName(packageName, className));
        intent.setDataAndType(record.getUri(activity), record.getMimeType(activity));
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        return intent;
    }

    private void displayDownloadViewerDialog(Activity activity, FileRecord record) {
        String message;
        if (record.isImageFileType()) {
            message = activity.getResources().getString(R.string.need_to_download_picture_viewer);
        } else if (record.isVideoFileType()) {
            message = activity.getResources().getString(R.string.need_to_download_video_viewer);
        } else {
            return;
        }
        DownloadGear360ViewerDialog.getInstance(message).show(activity.getFragmentManager(), "download_gear360");
    }

    private void updateRecentFile(Activity activity, FileRecord record) {
        FileUtils.updateRecentFile(activity, record);
    }
}
